ORG	0000H
LJMP MAIN
ORG 000BH
LJMP time_0_interrupt
ORG 001BH
LJMP time_1_interrupt

; define timer start
TIMER_0_MS EQU 0DC00H   ;10ms timer calculation method in 12T mode is (65536-FOSC/12/100)
TIMER_1_MS EQU 0FC66H   ;1ms timer calculation method in 12T mode is (65536-FOSC/12/1000)
; define timer end

; define sys reset io start
DU  EQU P2.0
WE  EQU P2.1
CS88  EQU P2.2
DS1302  EQU P2.7
LCD1602  EQU P2.5
; define sys reset io end

; define your lcd1602 io start
LCD_RS      EQU P1.0    ; -> LCD_RS
LCD_RW      EQU P1.1    ; -> LCD_RW
LCD_EN      EQU P2.5    ; -> EN
LCD_PORT    EQU P0      ; -> LCD_PORT
; define your lcd1602 io end

; define your ds1302 io start
DS1302_SCK  EQU P1.7    ; -> DS1302_SCK
DS1302_DATA EQU P2.7    ; -> DS1302_DATA
DS1302_RST  EQU P1.2    ; -> DS1302_RST
; define your ds1302 io end

; define ds1302 reg start
DS1302_SEC_REG EQU 080H
DS1302_MIN_REG EQU 082H
DS1302_HOUR_REG EQU 084H
DS1302_MDAY_REG EQU 086H
DS1302_MONTH_REG EQU 088H
DS1302_WDAY_REG EQU 08AH
DS1302_YEAR_REG EQU 08CH
DS1302_PROTECT_REG EQU 08EH
; define ds1302 reg end

; define your i_key io start
I_KEY_0 EQU P3.0
I_KEY_1 EQU P3.1
I_KEY_2 EQU P3.2
I_KEY_3 EQU P3.3
I_KEY_4 EQU P3.4
I_KEY_5 EQU P3.5
I_KEY_6 EQU P3.6
I_KEY_7 EQU P3.7
; define your i_key io end

MAIN:
    ; init sys start
    ACALL DELAY
    ACALL sys_reset
    ACALL sys_reg_init
    ACALL lcd1602_init
    ACALL time_0_init
    ACALL time_1_init
    SETB lcd_dis_mode
    ; ACALL ds1302_write_enable
    ; MOV ds1302_write_cmd_buff, #DS1302_MIN_REG
    ; MOV ds1302_write_data_buff, #005H
    ; ACALL ds1302_write_to_device
    ; ACALL ds1302_write_disable
    
    ; CLR lcd_dis_mode
    ; init sys end
LOOP:
    ; handle loop proc start
    ACALL i_key_scan
    JB lcd_dis_mode, clock_dis_task
    JNB lcd_dis_mode, time_count_task

;     MOV R0,#30
; delay_loop:
;     ACALL DELAY
;     DJNZ R0, delay_loop

    ; handle loop proc end
    AJMP LOOP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; sys_reset proc
sys_reset:
    CLR LCD1602
    CLR DS1302
    SETB CS88
    SETB WE
    MOV P0, #0FFH
    CLR WE
    CLR CS88
    RET

; sys_reg_init func
sys_reg_init:
    MOV R0, #00H
    MOV R1, #00H
    MOV R2, #00H
    MOV A, #00H
    RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; clock_dis_task func
clock_dis_task:
    MOV A, timer_count_1
    MOV B, #255  ; 255ms
    DIV AB
    MOV A, B
    CJNE A, #0, clock_end
    ACALL lcd1602_dis_time
    clock_end:
    AJMP LOOP

; time_count_task func
time_count_task:
    MOV A, timer_count_1
    MOV B, #10  ; 10ms
    DIV AB
    MOV A, B
    CJNE A, #0, time_count_end
    ACALL lcd1602_dis_count_time
    time_count_end:
    AJMP LOOP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; i_key_scan proc
i_key_scan:
    JNB I_KEY_0, i_key_0_event
    JNB I_KEY_1, i_key_1_event
    JNB I_KEY_2, i_key_2_event
    JNB I_KEY_3, i_key_3_event
    RET

; i_key_0_event proc
i_key_0_event:
    i_key_0_event_:
        ACALL DELAY
        JNB I_KEY_0, i_key_0_event_
    ; handle key0 event start
    ACALL lcd1602_clear         ; clear lcd dis_buff
    MOV time_count_isr, #0      ; clear time_count_isr
    MOV time_count_isr+1, #0    ; clear time_count_isr
    CPL lcd_dis_mode            ; switch dis_mode
    CPL P1.6                    ; switch mode led
    ; handle key0 event end
    RET

; i_key_1_event proc
i_key_1_event:
    i_key_1_event_:
        ACALL DELAY
        JNB I_KEY_1, i_key_1_event_
    ; handle key1 event start
    JNB lcd_dis_mode, add_time_count_num
    i_key_1_event_end1_:
        LCALL update_time_count
    i_key_1_event_end2_:
    ; handle key1 event end
    RET

; i_key_2_event proc
i_key_2_event:
    i_key_2_event_:
        ACALL DELAY
        JNB I_KEY_2, i_key_2_event_
    ; handle key2 event start
    JNB lcd_dis_mode, add_time_count_buff_num
    i_key_2_event_end1_:
    ; handle key2 event end
    RET

; i_key_3_event proc
i_key_3_event:
    i_key_3_event_:
        ACALL DELAY
        JNB I_KEY_3, i_key_3_event_
    ; handle key3 event start
    MOV time_count_num, #0
    MOV time_count_buff_num, #0
    MOV time_count_isr, #0
    MOV time_count_isr+1, #0
    MOV time_count_1, #0
    MOV time_count_1+1, #0
    MOV time_count_2, #0
    MOV time_count_2+1, #0
    MOV time_count_3, #0
    MOV time_count_3+1, #0
    MOV time_count_4, #0
    MOV time_count_4+1, #0
    MOV time_count_5, #0
    MOV time_count_5+1, #0
    MOV time_count_6, #0
    MOV time_count_6+1, #0
    ; handle key3 event end
    RET

add_time_count_buff_num:
    CPL P1.3
    INC time_count_buff_num
    MOV A, time_count_buff_num
    CJNE A, #7, i_key_2_event_end1_
    MOV time_count_buff_num, #1
    LJMP i_key_2_event_end1_

add_time_count_num:                 ; add_time_count_num func
    CPL P1.3                        ; switch mode led
    INC time_count_num              ; time_count_num + 1
    MOV A, time_count_num           ; set ACC
    CJNE A, #7, i_key_1_event_end1_   ; check time_count_num >? 9
    MOV time_count_num, #1          ; clear time_count_num
    LJMP i_key_1_event_end1_          ; jmp i_key_1_event_end

update_time_count:
    time_count_return_1_:
        MOV A, #1
        CJNE A, time_count_num, time_count_return_2_
        MOV time_count_1, time_count_isr
        MOV time_count_1+1, time_count_isr+1
    time_count_return_2_:
        MOV A, #2
        CJNE A, time_count_num, time_count_return_3_
        MOV time_count_2, time_count_isr
        MOV time_count_2+1, time_count_isr+1
    time_count_return_3_:
        MOV A, #3
        CJNE A, time_count_num, time_count_return_4_
        MOV time_count_3, time_count_isr
        MOV time_count_3+1, time_count_isr+1
    time_count_return_4_:
        MOV A, #4
        CJNE A, time_count_num, time_count_return_5_
        MOV time_count_4, time_count_isr
        MOV time_count_4+1, time_count_isr+1
    time_count_return_5_:
        MOV A, #5
        CJNE A, time_count_num, time_count_return_6_
        MOV time_count_5, time_count_isr
        MOV time_count_5+1, time_count_isr+1
    time_count_return_6_:
        MOV A, #6
        CJNE A, time_count_num, time_count_return_
        MOV time_count_6, time_count_isr
        MOV time_count_6+1, time_count_isr+1
    RET
time_count_return_:
    LJMP i_key_1_event_end2_

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; time_0_init func
time_0_init:
    MOV TMOD, #011H             ; set timer0 as mode1 (16-bit)
    MOV TH0, #HIGH TIMER_0_MS   ; initial timer0 high byte
    MOV TL0, #LOW TIMER_0_MS    ; initial timer0 low byte
    SETB ET0                    ; set ET0
    SETB EA                     ; set EA
    SETB TR0                    ; set TR0
    SETB P1.3
    RET

; time_1_init func
time_1_init:
    MOV timer_count_1, #250     ; L -> 250ms
    MOV timer_count_1+1, #4     ; H -> 4*250ms = 1000ms
    ; MOV TMOD, #010H             ; set timer1 as mode1 (16-bit)
    MOV TH1, #HIGH TIMER_1_MS   ; initial timer1 high byte
    MOV TL1, #LOW TIMER_1_MS    ; initial timer1 low byte
    SETB ET1                    ; set ET1
    SETB EA                     ; set EA
    SETB TR1                    ; set TR1
    SETB P1.4
    RET

; time_0 interrupt
time_0_interrupt:
    MOV TH0, #HIGH TIMER_0_MS   ; initial timer1 high byte
    MOV TL0, #LOW TIMER_0_MS    ; initial timer1 low byte
    CPL     P1.5                ; work LED flash
    PUSH ACC                    ; protect ACC
    PUSH PSW                    ; protect PSW
    MOV A, time_count_isr       ; set ACC
    CJNE A, #100, add_ms        ; check time_count_isr_L is 100 ?
    MOV time_count_isr, #0      ; clear time_count_isr_L
    MOV A, time_count_isr+1     ; set ACC
    CJNE A, #100, add_sec       ; check time_count_isr_H is 100 ?
    MOV time_count_isr+1, #0    ; clear time_count_isr_H
    time0_interrupt_exit:       ; time0 RETI func
        POP PSW                 ; release PSW
        POP ACC                 ; release ACC
    RETI                        ; return RET

add_ms:                         ; add_ms func
    INC time_count_isr          ; time_count_isr_L + 1
    AJMP time0_interrupt_exit   ; jmp to time0_interrupt_exit
add_sec:                        ; add_sec func
    INC time_count_isr+1        ; time_count_isr_L + 1
    AJMP time0_interrupt_exit   ; jmp to time0_interrupt_exit

; time_1 interrupt
time_1_interrupt:
    MOV TH1, #HIGH TIMER_1_MS                       ; initial timer1 high byte
    MOV TL1, #LOW TIMER_1_MS                        ; initial timer1 low byte
    DJNZ timer_count_1, time1_interrupt_exit        ; timer_count_1_L -1
    DJNZ timer_count_1+1, time1_interrupt_exit      ; timer_count_1_H -1
    CPL     P1.4                                    ; work LED flash -> 1s
    MOV timer_count_1, #250                         ; L -> 250ms
    MOV timer_count_1+1, #4                         ; H -> 4*250ms = 1000ms
    time1_interrupt_exit: RETI                      ; return RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; lcd1602_check_busy func
lcd1602_check_busy: 
	MOV LCD_PORT, #0FFH
	CLR LCD_RS
	SETB LCD_RW
	SETB LCD_EN
	JB LCD_PORT.7, lcd1602_check_busy
    CLR LCD_EN
    CLR LCD_RW
	RET

; lcd1602_write_data func
lcd1602_write_data:
    ACALL lcd1602_check_busy
    MOV LCD_PORT, A
    SETB LCD_RS
    SETB LCD_EN
    CLR LCD_EN
    ; ACALL DELAY
    RET

; lcd1602_write_cmd func
lcd1602_write_cmd:
    ACALL lcd1602_check_busy
    MOV     LCD_PORT, A
    CLR     LCD_RS
    SETB    LCD_EN
    CLR     LCD_EN
    ; ACALL   DELAY
    RET

; lcd1602_clear func
lcd1602_clear:
    MOV A, #01H
    ACALL lcd1602_write_cmd
    RET

; lcd1602_init func
lcd1602_init:
    CLR LCD_RW
    MOV A, #38H
    ACALL lcd1602_write_cmd
    ACALL DELAY
    MOV A, #38H
    ACALL lcd1602_write_cmd
    ACALL DELAY
    MOV A, #38H
    ACALL lcd1602_write_cmd
    ACALL DELAY
    MOV A, #38H
    ACALL lcd1602_write_cmd
    MOV A, #08H
    ACALL lcd1602_write_cmd
    MOV A, #01H
    ACALL lcd1602_write_cmd
    MOV A, #06H
    ACALL lcd1602_write_cmd
    MOV A, #0EH
    ACALL lcd1602_write_cmd
    RET

; lcd1602_set_position func
lcd1602_set_position:
    MOV A, lcd_y_position_var
    CJNE A, #1, lcd1602_set_y2
    MOV A, #0C0H
    ORL A, lcd_x_position_var
    ACALL lcd1602_write_cmd     ; write position_buff
    RET
lcd1602_set_y2:
    MOV A, #080H
    ORL A, lcd_x_position_var
    ACALL lcd1602_write_cmd     ; write position_buff
    RET

; lcd1602_dis_char func
lcd1602_dis_char:
    ACALL lcd1602_set_position
    MOV A, lcd_char_buff_var
    ACALL lcd1602_write_data
    RET

; lcd1602_dis_time func
lcd1602_dis_time:
    ; dis date_layout start
    MOV lcd_x_position_var, #0
    MOV lcd_y_position_var, #0
    MOV lcd_char_buff_var, #'D'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #':'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'2'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'0'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #6
    MOV lcd_char_buff_var, #'-'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #9
    MOV lcd_char_buff_var, #'-'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #13
    MOV lcd_char_buff_var, #'W'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'-'
    ACALL lcd1602_dis_char
    ; dis date_layout end

    ; dis ds1302 year start
    MOV ds1302_write_cmd_buff, #DS1302_YEAR_REG
    ACALL ds1302_write_read_from_device
    MOV A, ds1302_write_read_data_buff
    SWAP A
    ANL A, #0FH
    ORL A, #30H
    MOV lcd_x_position_var, #4
    MOV lcd_char_buff_var, A
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV A, ds1302_write_read_data_buff
    ANL A, #0FH
    ORL A, #30H
    MOV lcd_char_buff_var, A
    ACALL lcd1602_dis_char
    ; dis ds1302 year end

    ; loop dis ds1302 month-mday start
    MOV lcd_loop_dis, #2    ; hour min sec
    MOV lcd_x_position_var, #7
    MOV ds1302_write_cmd_buff, #DS1302_MONTH_REG
    dis_ds1302_date:
        ACALL ds1302_write_read_from_device
        MOV A, ds1302_write_read_data_buff
        SWAP A
        ANL A, #0FH
        ORL A, #30H
        MOV lcd_char_buff_var, A
        ACALL lcd1602_dis_char
        INC lcd_x_position_var
        MOV A, ds1302_write_read_data_buff
        ANL A, #0FH
        ORL A, #30H
        MOV lcd_char_buff_var, A
        ACALL lcd1602_dis_char
        DEC ds1302_write_cmd_buff
        DEC ds1302_write_cmd_buff
        INC lcd_x_position_var
        INC lcd_x_position_var
    DJNZ lcd_loop_dis, dis_ds1302_date
    ; loop dis ds1302 month-mday end

    ; dis ds1302 wday start
    MOV ds1302_write_cmd_buff, #DS1302_MDAY_REG
    ACALL ds1302_write_read_from_device
    MOV A, ds1302_write_read_data_buff
    ORL A, #30H
    MOV lcd_x_position_var, #15
    MOV lcd_char_buff_var, A
    ACALL lcd1602_dis_char
    ; dis ds1302 wday end

    ; dis time_layout start
    MOV lcd_x_position_var, #0
    MOV lcd_y_position_var, #1
    MOV lcd_char_buff_var, #'T'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'i'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'m'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'e'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #':'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #7
    MOV lcd_char_buff_var, #':'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #10
    MOV lcd_char_buff_var, #':'
    ACALL lcd1602_dis_char
    ; dis time_layout end

    ; loop dis ds1302 time start
    MOV lcd_loop_dis, #3    ; hour min sec
    MOV lcd_x_position_var, #5
    MOV ds1302_write_cmd_buff, #DS1302_HOUR_REG
    dis_ds1302_time:
        ACALL ds1302_write_read_from_device
        MOV A, ds1302_write_read_data_buff
        SWAP A
        ANL A, #0FH
        ORL A, #30H
        MOV lcd_char_buff_var, A
        ACALL lcd1602_dis_char
        INC lcd_x_position_var
        MOV A, ds1302_write_read_data_buff
        ANL A, #0FH
        ORL A, #30H
        MOV lcd_char_buff_var, A
        ACALL lcd1602_dis_char
        DEC ds1302_write_cmd_buff
        DEC ds1302_write_cmd_buff
        INC lcd_x_position_var
        INC lcd_x_position_var
    DJNZ lcd_loop_dis, dis_ds1302_time
    ; loop dis ds1302 time end
    RET

lcd1602_dis_count_time:
    ; dis time_layout start
    MOV lcd_x_position_var, #0
    MOV lcd_y_position_var, #0
    MOV lcd_char_buff_var, #'C'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'O'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'U'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'N'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'T'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'-'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV A, time_count_num
    ORL A, #30H
    MOV lcd_char_buff_var, A
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #9
    MOV lcd_char_buff_var, #'S'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'H'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'O'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'W'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV lcd_char_buff_var, #'-'
    ACALL lcd1602_dis_char
    INC lcd_x_position_var
    MOV A, time_count_buff_num
    ORL A, #30H
    MOV lcd_char_buff_var, A
    ACALL lcd1602_dis_char

    MOV lcd_x_position_var, #2
    MOV lcd_y_position_var, #1
    MOV lcd_char_buff_var, #':'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #5
    MOV lcd_char_buff_var, #'m'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #6
    MOV lcd_char_buff_var, #'s'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #11
    MOV lcd_char_buff_var, #':'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #14
    MOV lcd_char_buff_var, #'m'
    ACALL lcd1602_dis_char
    MOV lcd_x_position_var, #15
    MOV lcd_char_buff_var, #'s'
    ACALL lcd1602_dis_char
    ; dis time_layout end

    ; dis time count 00:00 start
    ; display 10s
    MOV lcd_x_position_var, #0
    MOV A, time_count_isr+1
    MOV B, #10
    DIV AB
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; display 1s
    MOV lcd_x_position_var, #1
    MOV A, time_count_isr+1
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; display 100ms
    MOV lcd_x_position_var, #3
    MOV A, time_count_isr
    MOV B, #10
    DIV AB
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; display 10ms
    MOV lcd_x_position_var, #4
    MOV A, time_count_isr
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; dis time count 00:00 end

    ; dis time buff count 00:00 start
    MOV A, time_count_buff_num
    dis_buff_num_break0_:
        CJNE A, #1, dis_buff_num_break1_
        MOV R0, time_count_1
        MOV R1, time_count_1+1
    dis_buff_num_break1_:
        CJNE A, #2, dis_buff_num_break2_
        MOV R0, time_count_2
        MOV R1, time_count_2+1
    dis_buff_num_break2_:
        CJNE A, #3, dis_buff_num_break3_
        MOV R0, time_count_3
        MOV R1, time_count_3+1
    dis_buff_num_break3_:
        CJNE A, #4, dis_buff_num_break4_
        MOV R0, time_count_4
        MOV R1, time_count_4+1
    dis_buff_num_break4_:
        CJNE A, #5, dis_buff_num_break5_
        MOV R0, time_count_5
        MOV R1, time_count_5+1
    dis_buff_num_break5_:
        CJNE A, #6, dis_buff_num_break6_
        MOV R0, time_count_6
        MOV R1, time_count_6+1
    dis_buff_num_break6_:

    ; display 10s
    MOV lcd_x_position_var, #9
    MOV A, R1
    MOV B, #10
    DIV AB
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; display 1s
    MOV lcd_x_position_var, #10
    MOV A, R1
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; display 100ms
    MOV lcd_x_position_var, #12
    MOV A, R0
    MOV B, #10
    DIV AB
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; display 10ms
    MOV lcd_x_position_var, #13
    MOV A, R0
    MOV B, #10
    DIV AB
    ORL B, #30H
    MOV lcd_char_buff_var, B
    ACALL lcd1602_dis_char
    ; dis time buff count 00:00 end
    
    RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; ds1302_write_to_device proc [ds1302_write_cmd_buff = cmd, ds1302_write_data_buff = write_data]
ds1302_write_to_device:
    CLR DS1302_RST  ; clear rst
    CLR DS1302_SCK  ; clear clk
    SETB DS1302_RST ; set rst
    NOP             ; wait Tcc
    ; write 1 ds1302_write_cmd start
    MOV R0, #8      ; for(8)
    MOV A, ds1302_write_cmd_buff    ; set write cmd buff
ds1302_write_cmd:     
    MOV C, ACC.0                    ; update C bit
    MOV DS1302_DATA, C              ; set ds1302_data_io
    NOP                             ; wait Tcdh
    SETB DS1302_SCK                 ; set ds1302_clk
    NOP                             ; wait Tcdh
    CLR DS1302_SCK                  ; clear clk
    RR A                            ; move right
    DJNZ R0, ds1302_write_cmd       ; for(8)
    ; write 1 ds1302_write_cmd end
    ; write 1 ds1302_write_data start
    MOV A, ds1302_write_data_buff   ; set write data buff
    MOV R0, #8                      ; for(8)
ds1302_write_data:
    MOV C, ACC.0                ; update C bit
    MOV DS1302_DATA, C          ; set ds1302_data_io
    NOP                         ; wait Tcdh
    SETB DS1302_SCK             ; set ds1302_clk
    NOP                         ; wait Tcdh
    CLR DS1302_SCK              ; clear clk
    RR A                        ; move right
    DJNZ R0, ds1302_write_data  ; for(8)
    ; write 1 ds1302_write_data end
    CLR DS1302_SCK              ; clear clk
    CLR DS1302_RST              ; clear rst
    RET

; ds1302_write_to_device proc [ds1302_write_cmd_buff = reg, ds1302_write_data_buff = read_data]
ds1302_write_read_from_device:
    CLR DS1302_RST  ; clear rst
    CLR DS1302_SCK  ; clear clk
    SETB DS1302_RST ; set rst
    NOP             ; wait Tcc
    ; write 1 ds1302_write_cmd
    MOV A, ds1302_write_cmd_buff    ; set write cmd buff
    ORL A, #001H                    ; set write cmd buff
    MOV R0, #8
ds1302_write_read_cmd:
    MOV C, ACC.0                    ; update C bit
    MOV DS1302_DATA, C              ; set ds1302_data_io
    NOP                             ; wait Tcdh
    SETB DS1302_SCK                 ; set ds1302_clk
    NOP                             ; wait Tcdh
    CLR DS1302_SCK                  ; clear clk
    RR A                            ; move right
    DJNZ R0, ds1302_write_read_cmd  ; for(8)
    ; write 1 ds1302_write_cmd end
    ; read 1 ds1302_data start
    MOV R0, #8  ; for(8)
    MOV A, #0   ; clear A
ds1302_write_read_data:
    MOV C, DS1302_DATA                  ; update C bit
    RRC A                               ; A&C_bit >> 1
    SETB DS1302_SCK                     ; set ds1302_clk
    NOP                                 ; wait Tcdh
    CLR DS1302_SCK                      ; clear clk
    DJNZ R0, ds1302_write_read_data     ; for(8)
    MOV ds1302_write_read_data_buff, A  ; update ds1302_write_read_data_buff
    ; read 1 ds1302_data end
    CLR DS1302_SCK  ; clear ds1302_sck
    CLR DS1302_RST  ; clear ds1302_rst
    RET

; ds1302_write_enable proc
ds1302_write_enable:
    MOV ds1302_write_cmd_buff, #DS1302_PROTECT_REG
    MOV ds1302_write_data_buff, #000H
    ACALL ds1302_write_to_device
    RET

; ds1302_write_disable proc
ds1302_write_disable:
    MOV ds1302_write_cmd_buff, #DS1302_PROTECT_REG
    MOV ds1302_write_data_buff, #080H
    ACALL ds1302_write_to_device
    RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; delay 50ms
DELAY:	MOV	    R1, #50	    ;APPROX 50 mS	;50 or higher for fast CPUs
HERE2:	MOV	    R2, #255    ;R4=255
HERE:	DJNZ    R2, HERE	;stay untill R4 becomes 0
	    DJNZ 	R1, HERE2
	    RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



DSEG AT 30H
    ; define lcd1602 data start
    lcd_x_position_var: DS 1
    lcd_y_position_var: DS 1
    lcd_char_buff_var: DS 1
    lcd_loop_dis: DS 1
    ; define lcd1602 data end

    ; define ds1302 data start
    ds1302_write_cmd_buff: DS 1
    ds1302_write_data_buff: DS 1
    ds1302_write_read_data_buff: DS 1
    ; define ds1302 data end

    ; define timer data start
    timer_count_1: DS 2
    ; define timer data end

    ; define timer count data start
    time_count_num: DS 1
    time_count_buff_num: DS 1
    time_count_isr: DS 2
    time_count_1: DS 2
    time_count_2: DS 2
    time_count_3: DS 2
    time_count_4: DS 2
    time_count_5: DS 2
    time_count_6: DS 2
    ; define timer count data end

BSEG AT 50H
    lcd_dis_mode: DBIT 1

END






